﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Scan.PDAService;
using Scan.Controls;

namespace Scan
{
    public partial class ReferInventoryView : BaseView
    {
        // 商品数据
        private Inventory[] gInventorys = null;
        public Inventory[] Inventorys
        {
            get { return gInventorys; }
            set { gInventorys = value; }
        }

        public ReferInventoryView()
        {
            InitializeComponent();
            this.Title = "商品选择";

            // 往ListView添加可以排序的列
            this.SuspendLayout();
            this.ResumeLayout(false);
        }

        public override void OnLoad()
        {
            if (needClearView)
            {
                needClearView = false;
                lstData.Items.Clear();
                txtCode.Text = "";
                txtName.Text = "";
            }

            lstData.Focus();
            if (lstData.Items.Count > 0)
            {
                lstData.Items[0].Selected = true;
            }
        }

        protected override void FocusControl(bool forward)
        {

        }

        // 绑定表格数据
        public void DataBind(Inventory[] Inventorys)
        {
            lstData.Items.Clear();
            if (Inventorys != null && Inventorys.Length != 0)
            {
                foreach (Inventory Inventory in Inventorys)
                {
                   // ListViewItem item = new ListViewItem(new string[] { Inventory.BarCode, Inventory.InvName, Inventory.InvStd, Inventory.InvType, Inventory.InvCode });
                   // lstData.Items.Add(item);
                }
            }
            else
            {
                MessageBox.Show("没有获取到数据！", this.Title);
            }
        }

        // 查找商品
        public void Find(string BarCode, string InvCode, string InvName)
        {
            gInventorys = ArchivesInventorys.GetInventorys(true, BarCode, InvCode, InvName);
            DataBind(gInventorys);
        }

        // 返回
        public override void Escape(object sender, EventArgs e)
        {
            this.ParentForm.LoadView(this.ResultView.GetType(), null, true, false);
        }

        // 确定，S2键
        public override void S2(object sender, EventArgs e)
        {
            int SelectIndex = 0;
            for (int i = 0; i < lstData.Items.Count; i++)
            {
                if (lstData.Items[i].Selected)
                {
                    SelectIndex = i;
                    break;
                }
            }
            this.ResultView.ReturnProc(true, ReferType.Inventory, gInventorys[SelectIndex]);
            this.ParentForm.LoadView(this.ResultView.GetType(), null, true, false);
        }

        // 定位、过滤编码和名称
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            /*
            ListViewItem foundItem = lstData.FocusedItem .FindItemWithText(textBox1.Text,false,0,true); 
            if (foundItem!=null) 
            {
                lstData.TopItem = foundItem;
                lstData.FindItemWithText(textBox1.Text, false, 0, true).BackColor = Color.Beige; 
            }
             * */
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
